package searchteam.beans.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.*;

import searchteam.client.beansMirrors.PaperMirror;
import searchteam.client.beansMirrors.PersonMirror;

@Entity
@Table(name="PAPERS")
public class Paper implements Serializable {

	private static final long serialVersionUID = -687521212822752124L;
	
	private int id;
	private String title;
	private String state;
	private Collection<Person> authors;
	
	private PaperMirror loaded;
	
	public Paper() {
		authors = new ArrayList<Person>();
	}
	
	public Paper(String title, String state) {
		this.title = title;
		this.state = state;
		authors = new ArrayList<Person>();
	}

	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	@ManyToMany
	public Collection<Person> getAuthors() {
		return authors;
	}

	public void setAuthors(Collection<Person> authors) {
		this.authors = authors;
	}

	public void addAuthor(Person author) {
		authors.add(author);
	}

	public void removeAuthor(Person author) {
		authors.remove(author);
	}
	
	@Transient
	public PaperMirror getMirror() {
		if(loaded == null) {
			loaded = new PaperMirror();
			loaded.setId(id);
			loaded.setState(state);
			loaded.setTitle(title);
			
			Collection<PersonMirror> authorMirrors = new ArrayList<PersonMirror>();
			for(Person author : authors) {
				authorMirrors.add(author.getMirror());
			}
			loaded.setAuthors(authorMirrors);
		}
		
		return loaded;
	}
	
}
